
//  YHComposeBarView.swift
//  XLWB
//
//  Created by key on 16/7/3.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

enum composeBarViewType: Int {

    // 图片
    case PictureType = 0
    // @
    case MentionType = 1
    // #
    case TrendType = 2
    // 笑脸
    case EmoticonType = 3
    // + 号
    case AddType = 4

}

class YHComposeBarView: UIView {
    
    //MARK: - 入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置UI
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 外界传递参数 判断笑脸图片
    
    var emoticonButton: UIButton?
    
    var isEmoticon: Bool = false {
        
        didSet {
            
            if isEmoticon { //自定义
                
                emoticonButton!.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                
                emoticonButton!.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
            }else{ //系统  
                
                emoticonButton!.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                
                emoticonButton!.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
            }
            
            
        }
        
    }
    
    
    //MARK: - 设置UI
    func setUpUI() {
        
        //添加子控件
        let pictureButton = addChildButton("compose_toolbar_picture", type: .PictureType)
        let mentionButton = addChildButton("compose_mentionbutton_background", type: .MentionType)
        let trendButton = addChildButton("compose_trendbutton_background", type: .TrendType)
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .EmoticonType)
        let addButton = addChildButton("compose_add_background", type: .AddType)

        
        //添加约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
            
        }
    }

    
    //添加控件方法
    func addChildButton(imageName: String, type: composeBarViewType) -> UIButton {
        
        let btn = UIButton()
        
        //设置属性
        btn.tag = type.rawValue
        
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        
        //添加响应方法
        btn.addTarget(self, action: "barButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        //添加
        addSubview(btn)
        
        
        return btn
        
        
    }
    
    
    //MARK: - Button响应方法
    //声明闭包
    var composeBarClosure: ((type: composeBarViewType) -> ())?
    func barButtonClick(button: UIButton) {
        
        
        //调用闭包
        composeBarClosure?(type: composeBarViewType(rawValue: button.tag)!)
        
    }
    
    
}
